package message.controller;

import message.model.User;
import message.service.UserService;
import message.util.DigestUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping("/user")
public class UserEditController {
	@Resource
	private UserService userService;
	private final Log logger = LogFactory.getLog(getClass());
	/**
	 * @author: �����
	 * @date: 2016��6��21��
	 * @param user
	 * @return ModelAndView
	 * @describe��mybatis�ļ��У���ǰ�˴���������ͨ��MD5���м��ܣ�����ȫ
	 * state ״̬Ϊ1����ʾ������0��ʾ����
	 */
	@RequestMapping("/registervalid")
	public ModelAndView registervalid(User user){
		ModelAndView mav=new ModelAndView();  //ʵ����һ��ModelAndView����
		mav.setViewName("register_suc");   // ��ʼ���߼���ͼΪregister_suc.jsp
		String qq=(System.currentTimeMillis()+"").substring(3,13);  //qq������ʱ��������ɡ�
		if(userService.findUserByQq(qq)!=null){  //�������qq���ɴ����������������
			logger.info("qq���ɴ���ԭ�������ظ�");
			this.registervalid(user);
		}
		user.setQq(qq);  //����qq
		user.setHeadpic("defaultheadpic.jpg");  //����Ĭ��ͷ��
		user.setState(1);  //��־״̬Ϊ1,����״̬
		if(userService.register(user)){
			logger.info(user.getUsername()+"ע��ɹ���QQΪ"+qq);
			mav.addObject("user", user);  //ע��ɹ�ת��ҳ��Ϊ��ʼ��ҳ�漴register_suc.jsp
		}else{
			logger.info(user.getUsername()+"ע��ɹ���ԭ��sql��䷢������");
			mav.addObject("register_fail","ע��ʧ�ܡ�ԭ���ڲ������������������Ժ��ٽ���ע��");
			mav.setViewName("register");  //��ע��ʧ��ת��ע��ҳ������ע��
		}
		return mav;
	}
	/**
	 * @author: �����
	 * @date: 2016��6��25��
	 * @param user
	 * @return ModelAndView
	 */
	@RequestMapping("/modifyInfor")
	public ModelAndView modifyInfor(User user,HttpSession session){
		ModelAndView mav=new ModelAndView();  //ʵ����һ��ModelAndView����
		User host=(User) session.getAttribute("host");
		mav.setViewName("hostinfor");   // ��ʼ���߼���ͼΪmodify_suc.jsp
		user.setUserid(host.getUserid());
		if(userService.updateUser(user)){
			logger.info(user.getQq()+"�޸��˻�����Ϣ");
			mav.addObject("modify_suc", "�޸ĳɹ�"); 
			User user1=userService.findUserById(host.getUserid());
			session.setAttribute("host", user1);
		}else{
			logger.info(user.getQq()+"�޸Ļ�����Ϣ�޸�ʧ�ܡ�ԭ��sql������");
			mav.addObject("modify_fail","�޸�ʧ�ܡ�ԭ���ڲ�����������ά�������Ժ��ٽ����޸�");
			mav.setViewName("modifyInfor");  //��ע��ʧ��ת���޸�ҳ�������޸�
		}
		return mav;
		
	}
	/**
		 * @author: �����
		 * @date: 2016��6��26��
		 * @param oldpassword ԭʼ����
		 * @param password ������
		 * @param repassword �������ظ�
		 * @param session
		 * @throws NoSuchAlgorithmException
		 * @describe�� �޸�����
	 */
	@RequestMapping("/modifyPass")
	public ModelAndView modifyPass(String oldpassword,String password,String repassword,HttpSession session) throws NoSuchAlgorithmException{
		ModelAndView mav=new ModelAndView();  //ʵ����һ��ModelAndView����
		User host=(User) session.getAttribute("host");
		User user=new User();
		if(!host.getPassword().equals(DigestUtil.getMD5(oldpassword.getBytes()))){
			mav.addObject("modifypass_fail","ԭʼ�����������");
			mav.setViewName("modifyInfor");  //���������ת���޸�ҳ�������޸�
		}
		else{
	    	user.setUserid(host.getUserid());
	    	user.setPassword(password);
			if(userService.updateUser(user)){
				session.invalidate();
				logger.info(user.getQq()+"�޸�������");
				mav.setViewName("redirect:/user/login");
			}else{
				logger.info(user.getQq()+"�޸������޸�ʧ�ܡ�ԭ��sql������");
				mav.addObject("modify_fail","�޸�ʧ�ܡ�ԭ���ڲ�����������ά�������Ժ��ٽ���ע��");
				mav.setViewName("modifyInfor");  //��ע��ʧ��ת���޸�ҳ�������޸�
			}
		}
		return mav;
	}
	
	/**
		 * @author: �����
		 * @date: 2016��6��26��
		 * @param file
		 * @param request
		 * @param session
		 * @describe���ϴ��ļ���ͷ��
	 */
	   @RequestMapping(value = "/updateheadpic")  
	    public ModelAndView upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,HttpSession session) {  
		   ModelAndView mav=new ModelAndView();
		   User host=(User) session.getAttribute("host");
	        String path =request.getSession().getServletContext().getRealPath("headpic");  
	        System.out.println(path);
	        String fileName = file.getOriginalFilename();  //���ͼƬ����
	        File targetFile = new File(path, fileName);  
	        if(!targetFile.exists()){  
	            targetFile.mkdirs();  
	        }  
	        //����  
	        try {  
	            file.transferTo(targetFile);  
	            User user=new User();
	            user.setUserid(host.getUserid());
		    	user.setHeadpic(fileName);
		    	if(userService.updateUser(user)){
		    		logger.info(user.getQq()+"�޸���ͷ��");
					mav.addObject("modify_suc", "�޸ĳɹ�"); 
					User user1=userService.findUserById(host.getUserid());
					session.setAttribute("host", user1);
					mav.setViewName("redirect:/user/feelallperson");
				}else{
					mav.addObject("modify_fail","�޸�ʧ�ܡ�ԭ���ڲ�����������ά�������Ժ��ٽ����޸�");
					mav.setViewName("updateheadpic");  //��ע��ʧ��ת���޸�ҳ�������޸�
				}
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return mav;  
	    }  
	
	

}
